#include "dialog.h"

#include <QDebug>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    initUi();
    initRetranslateUi();
    initSignalSlots();
}

void Dialog::initUi()
{
    this->setObjectName(QStringLiteral("Dialog"));
    vLayout = new QVBoxLayout(this);
    vLayout->setObjectName(QStringLiteral("vLayout"));

    groupBoxFont = new QGroupBox(this);
    groupBoxFont->setObjectName(QStringLiteral("groupBoxFont"));
    hLayout_font = new QHBoxLayout(groupBoxFont);
    hLayout_font->setObjectName(QStringLiteral("hLayout_font"));
    checkBoxUnder = new QCheckBox(groupBoxFont);
    checkBoxUnder->setObjectName(QStringLiteral("checkBoxUnder"));
    hLayout_font->addWidget(checkBoxUnder);
    checkBoxItalic = new QCheckBox(groupBoxFont);
    checkBoxItalic->setObjectName(QStringLiteral("checkBoxItalic"));
    hLayout_font->addWidget(checkBoxItalic);
    checkBoxBold = new QCheckBox(groupBoxFont);
    checkBoxBold->setObjectName(QStringLiteral("checkBoxBold"));
    hLayout_font->addWidget(checkBoxBold);
    vLayout->addWidget(groupBoxFont);

    groupBoxColor = new QGroupBox(this);
    groupBoxColor->setObjectName(QStringLiteral("groupBoxColor"));
    hLayout_color = new QHBoxLayout(groupBoxColor);
    hLayout_color->setObjectName(QStringLiteral("hLayout_color"));
    radioButtonBlack = new QRadioButton(groupBoxColor);
    radioButtonBlack->setObjectName(QStringLiteral("radioButtonBlack"));
    hLayout_color->addWidget(radioButtonBlack);
    radioButtonRed = new QRadioButton(groupBoxColor);
    radioButtonRed->setObjectName(QStringLiteral("radioButtonRed"));
    hLayout_color->addWidget(radioButtonRed);
    radioButtonBlue = new QRadioButton(groupBoxColor);
    radioButtonBlue->setObjectName(QStringLiteral("radioButtonBlue"));
    hLayout_color->addWidget(radioButtonBlue);
    radioButtonBlack->setChecked(true);
    vLayout->addWidget(groupBoxColor);

    plainTextEdit = new QPlainTextEdit(this);
    plainTextEdit->setObjectName(QStringLiteral("plainTextEdit"));
    vLayout->addWidget(plainTextEdit);

    hLayout_ops = new QHBoxLayout();
    hLayout_ops->setObjectName(QStringLiteral("hLayout_ops"));
    hSpacer_left = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    hLayout_ops->addItem(hSpacer_left);
    btnErase = new QPushButton(this);
    btnErase->setObjectName(QStringLiteral("btnErase"));
    iconErase.addFile(QStringLiteral(":/res/erase.png"), QSize(), QIcon::Normal, QIcon::Off);
    btnErase->setIcon(iconErase);
    hLayout_ops->addWidget(btnErase);
    btnNew = new QPushButton(this);
    btnNew->setObjectName(QStringLiteral("btnNew"));
    iconNew.addFile(QStringLiteral(":/res/new.png"), QSize(), QIcon::Normal, QIcon::Off);
    btnNew->setIcon(iconNew);
    hLayout_ops->addWidget(btnNew);
    hSpacer_right = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    hLayout_ops->addItem(hSpacer_right);
    btnConfirm = new QPushButton(this);
    btnConfirm->setObjectName(QStringLiteral("btnConfirm"));
    iconConfirm.addFile(QStringLiteral(":/res/confirm.png"), QSize(), QIcon::Normal, QIcon::Off);
    btnConfirm->setIcon(iconConfirm);
    hLayout_ops->addWidget(btnConfirm);
    btnExit = new QPushButton(this);
    btnExit->setObjectName(QStringLiteral("btnExit"));
    iconExit.addFile(QStringLiteral(":/res/exit.png"), QSize(), QIcon::Normal, QIcon::Off);
    btnExit->setIcon(iconExit);
    hLayout_ops->addWidget(btnExit);
    vLayout->addLayout(hLayout_ops);
}

void Dialog::initRetranslateUi()
{
    this->setWindowTitle(QCoreApplication::translate("Dialog", "信号与槽的使用", nullptr));
    groupBoxFont->setTitle(QCoreApplication::translate("Dialog", "字体", nullptr));
    checkBoxUnder->setText(QCoreApplication::translate("Dialog", "下划线", nullptr));
    checkBoxItalic->setText(QCoreApplication::translate("Dialog", "斜体", nullptr));
    checkBoxBold->setText(QCoreApplication::translate("Dialog", "粗体", nullptr));
    groupBoxColor->setTitle(QCoreApplication::translate("Dialog", "颜色", nullptr));
    radioButtonBlack->setText(QCoreApplication::translate("Dialog", "黑色", nullptr));
    radioButtonRed->setText(QCoreApplication::translate("Dialog", "红色", nullptr));
    radioButtonBlue->setText(QCoreApplication::translate("Dialog", "蓝色", nullptr));
    plainTextEdit->setPlainText(QCoreApplication::translate("Dialog",
                                                            "你好，这是一段测试文本内容。\n"
                                                            "你可以调整文本显示样式。", nullptr));
    btnErase->setText(QCoreApplication::translate("Dialog", "清空内容", nullptr));
    btnNew->setText(QCoreApplication::translate("Dialog", "追加内容", nullptr));
    btnConfirm->setText(QCoreApplication::translate("Dialog", "确定", nullptr));
    btnExit->setText(QCoreApplication::translate("Dialog", "退出", nullptr));
}

void Dialog::initSignalSlots()
{
    QObject::connect(btnConfirm, SIGNAL(clicked()), this, SLOT(accept()));
    QObject::connect(btnExit, SIGNAL(clicked()), this, SLOT(close()));

    QObject::connect(btnErase, SIGNAL(clicked(bool)), this, SLOT(on_btnErase_clicked(bool)));
    QObject::connect(btnNew, SIGNAL(clicked(bool)), this, SLOT(on_btnNew_clicked(bool)));

    QObject::connect(radioButtonBlack, SIGNAL(clicked()), this, SLOT(do_setFontColor()));
    QObject::connect(radioButtonRed, SIGNAL(clicked()), this, SLOT(do_setFontColor()));
    QObject::connect(radioButtonBlue, SIGNAL(clicked()), this, SLOT(do_setFontColor()));

    QObject::connect(checkBoxUnder, SIGNAL(clicked(bool)), this, SLOT(on_checkBoxUnder_clicked(bool)));
    QObject::connect(checkBoxItalic, SIGNAL(clicked(bool)), this, SLOT(on_checkBoxItalic_clicked(bool)));
    QObject::connect(checkBoxBold, SIGNAL(clicked(bool)), this, SLOT(on_checkBoxBold_clicked(bool)));
}

void Dialog::do_setFontColor()
{
    QPalette plet = plainTextEdit->palette();
    if (radioButtonBlack->isChecked()) {
        plet.setColor(QPalette::Text, Qt::black);
    } else if (radioButtonRed->isChecked()) {
        plet.setColor(QPalette::Text, Qt::red);
    } else if (radioButtonBlue->isChecked()) {
        plet.setColor(QPalette::Text, Qt::blue);
    } else {
        plet.setColor(QPalette::Text, Qt::black);
    }
    plainTextEdit->setPalette(plet);
}

void Dialog::on_btnErase_clicked(bool checked)
{
    plainTextEdit->clear();
}

void Dialog::on_btnNew_clicked(bool checked)
{
    QObjectList objList = this->children();
    for (int i = 0; i < objList.size(); ++i) {
        QObject *objPtr = objList.at(i);
        QString objName = QString(objPtr->objectName());
        const QMetaObject *metaPtr = objPtr->metaObject();
        QString className = QString(metaPtr->className());
        QString btnText = QStringLiteral(" ");
        if ( metaPtr->superClass()->className() == QStringLiteral("QAbstractButton") ) {
            QAbstractButton *btnPtr = qobject_cast<QAbstractButton *>(objPtr);
            btnText = btnPtr->text();
        }
        plainTextEdit->appendPlainText(objName +  QStringLiteral(" ") + className +  QStringLiteral(" ") + btnText);
    }
}

void Dialog::on_checkBoxUnder_clicked(bool checked)
{
    QFont font = plainTextEdit->font();
    font.setUnderline(checked);
    plainTextEdit->setFont(font);
}

void Dialog::on_checkBoxItalic_clicked(bool checked)
{
    QFont font = plainTextEdit->font();
    font.setItalic(checked);
    plainTextEdit->setFont(font);
}

void Dialog::on_checkBoxBold_clicked(bool checked)
{
    QFont font = plainTextEdit->font();
    font.setBold(checked);
    plainTextEdit->setFont(font);
}
